package com.jjw.controller;

import com.jjw.pojo.RegionStats;
import com.jjw.service.CoronaVirusDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
public class CVDController {
    @Autowired
    CoronaVirusDataService coronaVirusDataService;
    @RequestMapping("/index")
    public String index(Model model){
        List<RegionStats> regionStats = coronaVirusDataService.getRegionStats();
        int sum = regionStats.stream().mapToInt(RegionStats::getLatestTotalCases).sum();
        int diff = regionStats.stream().mapToInt(RegionStats::getDiffFromPrevDay).sum();
        model.addAttribute("sum",sum);
        model.addAttribute("diff",diff);
        return "index";
    }

    @RequestMapping("/state")
    public String state(Model model){
        model.addAttribute("cvds",coronaVirusDataService.getRegionStats());
        return "state";
    }

    @RequestMapping("/search")
    public String search(Model model, @RequestParam("search") String search){
        List<RegionStats> regionStats = coronaVirusDataService.getRegionStats();
        List<RegionStats> collect = regionStats.stream().filter(regionStats1 -> regionStats1.getCountry().equals(search)).collect(Collectors.toList());
        model.addAttribute("cvds",collect);
        return "state";
    }
}
